<?php
//商品控制器类
class GoodsController extends CommonController{
    private $model;
    public function __init(){
        parent::__init();
        $this->model=K("Goods");
    }
    //动作方法
    //
    public function index(){
        $goodsData=$this->model->get_data();
        $this->assign('goodsData',$goodsData);
        //显示视图
        $this->display();
    }
    public function add(){
        if(IS_POST){
            // $pic = Q('post.pic');
            // p($_POST);die();
            if($this->model->create()){
                $this->model->add_data();
                $this->success("增加成功",U("index"));
            }
            $this->error($this->model->error);
          
        }
    	// 获取所有分类数据
    	$cateData=K('category')->get_data();
    	$cateData= Data::tree($cateData,'cname','cid','pid');
        $this->assign('cateData',$cateData);
        // 获取所有品牌数据
    	$brandData=K('brand')->get_data();
        $this->assign('brandData',$brandData);
    	//显示视图
        $this->display();
    }
    /**
     * [getAttr 异步获取类型数据]
     * @return [type] [description]
     */
    public function getAttr(){
    	if(!IS_AJAX) $this->error('非法请求');
    	$tid = Q('post.tid');
    	$AttrData=K('TypeAttr')->get_data(array('tid'=>$tid));
    	// p($AttrData);die();
    	if ($AttrData) {
    		echo json_encode($AttrData);
    		die();
    	}else{
    		echo 0;
    		die();
    	}
    	
    }
    /**
     * [del 删除商品]
     * @return [type] [description]
     */
    public function del(){
        $gid =Q('get.gid',0, 'intval');
        // p($gid);die();
        if($this->model->del_data(array('gid'=>$gid))){
            $this->success('删除成功');
        }else{
            $this->error($this->model->error);
        }
    }

    public function edit(){
         if(IS_POST){
            // $pic = Q('post.pic');
            // p($_POST);die();
            if($this->model->create()){
                $this->model->add_data();
                $this->success("增加成功",U("index"));
            }
            $this->error($this->model->error);
          
        }
        // 获取当前商品的数据
        $gid = Q('get.gid',0,'intval');
        // 商品数据
        $oldData = $this->model->get_one_data(array('gid'=>$gid));
        $this->assign('oldData',$oldData);
        // 商品详情数据
        $DetailData = K('GoodsDetail')->get_one_data(array('gid'=>$gid));
        $this->assign('DetailData',$DetailData);
        // 获取所有分类数据
        $cateData=K('category')->get_data();
        $cateData= Data::tree($cateData,'cname','cid','pid');
        $this->assign('cateData',$cateData);
        // 获取所有品牌数据
        $brandData=K('brand')->get_data();
        $this->assign('brandData',$brandData);
        // 显示视图
        $this->display();
    }
}
